package com.smarttest.portal.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.smarttest.common.beans.PaginationResult;
import com.smarttest.common.beans.SFTPOPBean;
import com.smarttest.portal.mapper.AutoCasesMsgMapper;
import com.smarttest.portal.pojo.AutoCasesMsg;
import com.smarttest.portal.pojo.AutoCasesMsgExample;
import com.smarttest.portal.service.Autocase_Mgmt_Service;
import com.smarttest.portal.pojo.AutoCasesMsgExample.Criteria;
@Service("msgAuto")
public class Autocase_Mgmt_Msg_ServiceImpl implements Autocase_Mgmt_Service {
	@Autowired
	private AutoCasesMsgMapper mapper;

	@Override
	public int createCase(String name, String clazz, String scenes, String owner, String remark, int level,String url) {
		// TODO Auto-generated method stub
		AutoCasesMsg auto = new AutoCasesMsg();
		auto.setName(name);
		auto.setClazz(clazz);
		auto.setScens(scenes);
		auto.setOwner(owner);
		auto.setRemark(remark);
		auto.setLevel(level);
		auto.setCreated(new Date());
		auto.setUpdated(new Date());
		int i = mapper.insert(auto);
		return i;
	}

	@Override
	public PaginationResult searchCase(String name, String classes, String scenes, String owners, String searchvalue,
			int level, int pageSize, int toPage) {
		// TODO Auto-generated method stub
		AutoCasesMsgExample example = new AutoCasesMsgExample();
		if (null != searchvalue && !"".equals(searchvalue)) {
			example.or().andNameLike("%"+searchvalue+"%");
			example.or().andClazzLike("%"+searchvalue+"%");
			example.or().andScensLike("%"+searchvalue+"%");
			example.or().andOwnerLike("%"+searchvalue+"%");
		} else {
			Criteria criteria = example.createCriteria();
			if (null != name && !"".equals(name)) {
				criteria.andNameEqualTo(name);
			}
			if (null != classes && !"".equals(classes)) {
				criteria.andClazzEqualTo(classes);
			}
			if (null != scenes && !"".equals(scenes)) {
				criteria.andScensEqualTo(scenes);
			}
			if (null != owners && !"".equals(owners)) {
				criteria.andOwnerEqualTo(owners);
			}
			if (level > 0) {
				criteria.andLevelEqualTo(level);
			}
		}
		// 分页处理
		PageHelper.startPage(toPage, pageSize);
		List<AutoCasesMsg> list = mapper.selectByExample(example);
		// 创建一个返回值对象
		PaginationResult result = new PaginationResult();
		result.setRows(list);
		PageInfo<AutoCasesMsg> pageInfo = new PageInfo<AutoCasesMsg>(list);
		result.setTotal(pageInfo.getTotal());
		return result;
	}

	@Override
	public int updateCase(long id, String name, String clazz, String scenes, String owner, String remark, int level,String url) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		AutoCasesMsgExample example = new AutoCasesMsgExample();
		Criteria criteria = example.createCriteria();
		criteria.andIdEqualTo(id);
		// TODO Auto-generated method stub
		List<AutoCasesMsg> list = mapper.selectByExample(example);
		// 判断是否查询到结果
		if (list != null && list.size() > 0) {
			AutoCasesMsg each = list.get(0);
			each.setName(name);
			each.setClazz(clazz);
			each.setScens(scenes);
			each.setOwner(owner);
			each.setRemark(remark);
			each.setLevel(level);
			int result = mapper.updateByPrimaryKey(each);
			return result;
		} else {
			return 0;
		}
	}

	@Override
	public int deleteCase(List<Long> ids) {
		// TODO Auto-generated method stub
		int result = mapper.batchDelete(ids);
	    return result;
	}

	@Override
	public PaginationResult searchCaseData(int caseid, int level, String searchvalue, int pageSize,
			int toPage) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Map uploadCase(MultipartFile uploadFile) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public SFTPOPBean downLoadSingleCase(String url) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int softDeleteCase(List<Long> ids) {
		// TODO Auto-generated method stub
		return 0;
	}
}
